import jwt
class MyJwt:
    def __init__(self) -> None:
        self.secret_key = 'asdjildjliasjdliajsildasdas'

    # 生成token
    def jwt_encode(self,data):
        return jwt.encode(data,self.secret_key,algorithm='HS256')
    # 解析token
    def jwt_decode(self,token):
        try:
            payload = jwt.decode(token, self.secret_key, algorithm='HS256')
        except:
            payload = None
        return payload
    # 对比token
    def check_token(self,token):
        # 解析payload
        payload = self.jwt_decode(token)
        if payload:
            # 用payload重新生成token
            newtoken = self.jwt_decode(payload)
            if token == newtoken:
                return True
        return False

myjwt = MyJwt()
# import time
# data = {'data':{'userid':123,'exp':int(time.time())}}
# token = myjwt.jwt_encode(data)
# print(token)
# payload = myjwt.jwt_decode(token)
# print(payload)
# flag = myjwt.check_token(token)
# print(flag)